package com.beeantec.jnrerc.modules.common.service.impl;


import com.beeantec.jnrerc.core.constants.CacheKey;
import com.beeantec.jnrerc.core.constants.ConstantsMsg;
import com.beeantec.jnrerc.core.utils.RandomUtils;
import com.beeantec.jnrerc.core.utils.RedisUtils;
import com.beeantec.jnrerc.dto.response.SmsCheckRespDTO;
import com.beeantec.jnrerc.dto.response.SmsRespDTO;
import com.beeantec.jnrerc.modules.common.service.SmsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.util.ObjectUtils;
import org.springframework.web.client.RestTemplate;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;

@Service
public class SmsServiceImpl implements SmsService {
    protected Logger log = LoggerFactory.getLogger(getClass());



    @Autowired
    MqServiceImpl mqServiceImpl;

    @Autowired
    RedisUtils redisUtils;


    @Autowired
    RestTemplate restTemplate;


    @Override
    public String sendSms(String phone, Integer smsType) {
//        StringBuilder url = new StringBuilder("https://sdk2.028lk.com/utf8");
//        url.append("/BatchSend2.aspx?CorpID={CorpID}&Pwd={Pwd}&Mobile={Mobile}&Content={Content}");
//        Map<String, String> uriVariables = new HashMap<String, String>();
//        String sms = RandomUtils.getRandomNum(6);
//        String content = "您的短信验证码为"+ sms;
//        uriVariables.put("CorpID", "LC007698");
//        uriVariables.put("Pwd", "123456@");
//        uriVariables.put("Mobile", phone);
//        uriVariables.put("Content", content);
        StringBuilder url = new StringBuilder("https://yblm.books101.cn/api/index");
        url.append("/sed_msg_bd?mobile={Mobile}&code={Content}");
        Map<String, String> uriVariables = new HashMap<String, String>();
        String sms = RandomUtils.getRandomNum(6);
        String content =  sms;

        uriVariables.put("Mobile", phone);
        uriVariables.put("Content", content);
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=utf-8");
        headers.setContentType(type);
        HttpEntity requestEntity = new HttpEntity(headers);
        ResponseEntity<SmsRespDTO> result = restTemplate.exchange(url.toString(), HttpMethod.GET, requestEntity, SmsRespDTO.class, uriVariables);

        
        //redis连不上，暂时注释
        if (result.getBody().getCode()==1){
            String codeKey = MessageFormat.format(CacheKey.sms_code, phone, smsType);
            redisUtils.set(codeKey, sms, 300);
        }
        return result.getBody().getCode().toString();
    }

    @Override
    public SmsCheckRespDTO checkSmsCode(Integer smsType, String phone, String sms) {
        SmsCheckRespDTO smsRespDTO = new SmsCheckRespDTO();
        if (ObjectUtils.isEmpty(phone) || ObjectUtils.isEmpty(sms)) {
            smsRespDTO.setSuccess(false);
            smsRespDTO.setMsg(ConstantsMsg.sms_param_empty);
            return smsRespDTO;
        }
        String codeKey = MessageFormat.format(CacheKey.sms_code, phone, smsType);
        Object codeObject = redisUtils.get(codeKey);
        if (ObjectUtils.isEmpty(codeObject) ) {
            smsRespDTO.setSuccess(false);
            smsRespDTO.setMsg(ConstantsMsg.sms_code_expire);
            return smsRespDTO;
        }
        if (!sms.equals(codeObject.toString())) {
            smsRespDTO.setSuccess(false);
            smsRespDTO.setMsg(ConstantsMsg.sms_code_error_msg);
            return smsRespDTO;
        }
        redisUtils.del(codeKey);
        smsRespDTO.setSuccess(true);
        smsRespDTO.setMsg("");
        return smsRespDTO;
    }



}
